﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using _3TierDemo.BIZ;
using _3TierDemo.DTO;
using _3TierDemo.Common;
namespace _3TierDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            loadDataForGridViewSV();
            loadDataForComboBoxLop();
        }       
       
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtMa.Text == string.Empty)
                doInsert();
            else
                doUpdate();
            loadDataForGridViewSV();
            doClear();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            doDelete();
            loadDataForGridViewSV();
            doClear();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            doClear();
        }

        private void dgvSV_SelectionChanged(object sender, EventArgs e)
        {
            //lay duoc ma ve
            if (dgvSV.SelectedRows.Count > 0)
            {
                int ma = Convert.ToInt32(dgvSV.SelectedRows[0].Cells[0].Value);
                SVBIZ biz = new SVBIZ();
                SVDTO obj = biz.getByID(ma);
                txtMa.Text = obj.Ma.ToString();
                txtTen.Text = obj.Ten;
                txtTuoi.Text = obj.Tuoi.ToString();
                cboLop.SelectedValue = obj.MaLop;
            }
        }
        
        #region Methods

        public void loadDataForGridViewSV()
        {
            SVBIZ biz = new SVBIZ();
            dgvSV.DataSource = biz.getAll();
        }

        public void loadDataForComboBoxLop()
        {
            LopBIZ biz = new LopBIZ();
            cboLop.ValueMember = CONSTANT.MA;
            cboLop.DisplayMember = CONSTANT.TEN;
            cboLop.DataSource = biz.getAll();
        }
      
        public void doInsert()
        {
            SVDTO obj = loadDataFromControlsToDTO();
            SVBIZ biz = new SVBIZ();
            biz.insert(obj);
        }

        public void doUpdate()
        {
            SVDTO obj = new SVDTO();
            obj.Ma = Convert.ToInt32(txtMa.Text);
            obj.Ten = txtTen.Text;
            obj.Tuoi = Convert.ToInt32(txtTuoi.Text);
            obj.MaLop = Convert.ToInt32(cboLop.SelectedValue);

            SVBIZ biz = new SVBIZ();
            biz.update(obj);
        }

        public void doDelete()
        {
            int id = Convert.ToInt32(txtMa.Text);
            SVBIZ biz = new SVBIZ();
            biz.deleteByID(id);
        }

        public void doClear()
        {
            txtMa.Text = string.Empty;
            txtTen.Text = string.Empty;
            txtTuoi.Text = "";
            cboLop.SelectedIndex = 0;
        }

        private SVDTO loadDataFromControlsToDTO()
        {
            SVDTO obj = new SVDTO();
            obj.Ten = txtTen.Text;
            obj.Tuoi = Convert.ToInt32(txtTuoi.Text);
            obj.MaLop = Convert.ToInt32(cboLop.SelectedValue);
            return obj;
        }

        #endregion
        
    }
}
